package com.example.oss.runner;

import com.aliyun.oss.OSS;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class AppRunner implements ApplicationRunner {

    @Value("${alibaba.cloud.oss.bucket-name}")
    private String bucketName;

    @Resource
    private OSS ossClient;

    @Override
    public void run(ApplicationArguments args) {
        try {
            if (!ossClient.doesBucketExist(bucketName)) {
                ossClient.createBucket(bucketName);
            }
        } catch (Exception e) {
            System.err.println("oss handle bucket error: " + e.getMessage());
            System.exit(-1);
        }
    }
}
